local leicheng = fk.CreateSkill{
  name = "xiaobai__leicheng",
}

Fk:loadTranslationTable{
  ["xiaobai__leicheng"] = "垒城",
  [":xiaobai__leicheng"] = "当你使用装备牌时，可以选择一项：1.将上一名使用装备牌的角色的一张牌当【以逸待劳】对你使用。2.摸至四张牌，然后有角色下次使用装备时，其弃置你一张手牌。",
  ["#use_card_as_await_exhausted"] = "将 %dest 的一张牌当【以逸待劳】对你使用",
  ["#use_card_as_await_exhausted_empty"] = "将上一名角色的一张牌当【以逸待劳】对你使用",
  ["#draw2four"] = "摸至四张牌，然后有角色下次使用装备时，其弃置你一张手牌",
  ["@@leicheng_next"] = "垒城",
  ["#leicheng_choose2discard"] = "垒城：弃置 %dest 的一张手牌",
  ["#leicheng_chooseCard"] = "垒城：请选择 %dest 的一张手牌"
}

leicheng:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  priority = 2,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and data.card.type == Card.TypeEquip and player==target
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = nil
    local all_choices = {
      "#use_card_as_await_exhausted_empty",
      "#draw2four",
      "Cancel"
    }
    local choices = {all_choices[2],"Cancel"}
    room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
      if e.id ~= room.logic:getCurrentEvent().id then
        local use = e.data
        if use.card.type == Card.TypeEquip then
          to = use.from
          return true
        end
      end
    end, 1)
    
    local cost_data = {}
    if to~= nil and not to:isNude() then
      all_choices[1] = "#use_card_as_await_exhausted::"..to.id
      table.insert(choices,1,all_choices[1])
      cost_data.to = to
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = leicheng.name,
      prompt = "xiaobai__leicheng",
      cancelable = false,
      all_choices = all_choices})
    if choice~= "Cancel" then
      cost_data.choice = choice
      event:setCostData(self, table.simpleClone(cost_data))
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    if cost_data.choice:startsWith("#use_card_as_await_exhausted") then
      local to = cost_data.to
      local card = room:askToChooseCard(player, {
        target = to,
        flag = "he",
        skill_name = leicheng.name,
        prompt = "#leicheng_chooseCard::"..to.id})
      room:useVirtualCard("await_exhausted",{card},player,player,leicheng.name)
    else
      if player:getHandcardNum() < 4 then
        local draw_num = 4-player:getHandcardNum()
        room:drawCards(player,draw_num,leicheng.name)
      end
      room:setPlayerMark(player,"leicheng_next-trigger",1)
    end
  end
})

leicheng:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  priority = 1,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self,true) and ((player:getMark("@@leicheng_next") > 0 and data.card.type == Card.TypeEquip and player:getHandcardNum() > 0) or player:getMark("leicheng_next-trigger") > 0)
  end,
  frequency = Skill.Compulsory,
  on_use = function (self, event, target, player, data)
    if player:getMark("@@leicheng_next") > 0 then
      if not player:isNude() then
        local card  = player.room:askToChooseCard(target, {
          target = player,
          flag = "h",
          prompt = "xiaobai__leicheng","#leicheng_choose2discard::"..player.id,
          skill_name = leicheng.name})
        player.room:throwCard({card},"xiaobai__leicheng",player,target)
      end
      if player.dead then return end
      player.room:setPlayerMark(player,"@@leicheng_next",0)
    end
    if player:getMark("leicheng_next-trigger") > 0 then
      player.room:setPlayerMark(player,"leicheng_next-trigger",0)
      player.room:setPlayerMark(player,"@@leicheng_next",1)
    end
  end,
})

return leicheng